package com.yanqu.road.dao.impl.logic;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.entity.player.UserPatronsSkill;
import com.yanqu.road.entity.rank.ServerRank;
import com.yanqu.road.entity.rank.cross.CrossUserRank;
import com.yanqu.road.entity.tradewar.TradeWarTrackRecord;
import com.yanqu.road.entity.tradewar.TradeWarWanted;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ResultSetParseEntityManger {

    public static UserPatrons initUserPatrons(ResultSet rs) throws SQLException {
        UserPatrons data = new UserPatrons();
        data.setUserId(rs.getLong("userId"));
        data.setSkinId(rs.getInt("skinId"));
        data.setPatronsId(rs.getInt("patronsId"));
        data.setStageLv(rs.getInt("stageLv"));
        data.setBreakLv(rs.getInt("breakLv"));
        data.setLevel(rs.getInt("level"));
        data.setCookExp(rs.getLong("cookExp"));
        String value = rs.getString("ability");
        if(!StringUtils.isNullOrEmpty(value)) {
            data.setAbility(new BigInteger(value));
        }
        data.setQualification(rs.getLong("qualification"));
        value = rs.getString("addAbility");
        if(!StringUtils.isNullOrEmpty(value)) {
            data.setAddAbility(new BigInteger(value));
        }
        value = rs.getString("tradeWarAddEarnSpeed");
        if(!StringUtils.isNullOrEmpty(value)) {
            data.setTradeWarAddEarnSpeed(new BigInteger(value));
        }
        data.setUnionTradeSend(rs.getBoolean("unionTradeSend"));
        data.setResetTime(rs.getLong("resetTime"));
        data.setBaiYeExp(rs.getLong("baiYeExp"));
        data.setTradeWarLoseScoreReduce(rs.getLong("tradeWarLoseScoreReduce"));
        data.setChouSuanExp(rs.getLong("chouSuanExp"));
        data.setPromotionId(rs.getInt("promotionId"));
        data.setTeacherExp(rs.getLong("teacherExp"));
        String grandChildSpeed = rs.getString("grandChildSpeed");
        if (!StringUtils.isNullOrEmpty(grandChildSpeed)) {
            data.setGrandChildSpeed(new BigInteger(grandChildSpeed));
        }
        data.setLinkageSwitch(rs.getBoolean("isLinkageSwitch"));
        data.setKeepSakeAddition(rs.getLong("keepSakeAddition"));
        data.setKeepSakePatronsId(rs.getInt("keepSakePatronsId"));
        data.setKeepSakeTime(rs.getInt("keepSakeTime"));
        data.setItemAddQualification(rs.getInt("itemAddQualification"));
        return data;
    }

    public static UserPatronsSkill initUserPatronsSkill(ResultSet rs) throws SQLException {
        UserPatronsSkill data = new UserPatronsSkill();
        data.setUserId(rs.getLong("userId"));
        data.setPatronsId(rs.getInt("patronsId"));
        data.setSkillId(rs.getInt("skillId"));
        data.setSkillLv(rs.getInt("skillLv"));
        data.setType(rs.getInt("type"));
        data.setSkinSkillLevel(rs.getInt("skinSkillLevel"));
        data.setExtraParams(rs.getString("extraParams"));
        data.setIsDelete(rs.getInt("isDelete"));
        return data;
    }

    public static CrossUserRank initCrossUserRank(ResultSet rs) throws SQLException {
        CrossUserRank data = new CrossUserRank();
        data.setType(rs.getInt("activityId"));
        data.setRank(rs.getInt("rank"));
        data.setUserId(rs.getLong("userId"));
        String baseUserInfoStr = rs.getString("userBaseInfo");
        if(null == baseUserInfoStr){
            baseUserInfoStr = "";
        }
        UserBaseInfo userBaseInfo = JSON.parseObject(baseUserInfoStr, new TypeReference<UserBaseInfo>() {});
        if(null != userBaseInfo){
            data.setUserBaseInfo(userBaseInfo);
        }
        data.setLastUpdateTime(rs.getLong("lastUpdateTime"));
        String value = rs.getString("value");
        if(!StringUtils.isNullOrEmpty(value)) {
            data.setValue(new BigInteger(value));
        }
        return data;
    }

    public static ServerRank initCrossServerRank(ResultSet rs) throws SQLException {
        ServerRank data = new ServerRank();
        data.setActivityId(rs.getInt("activityId"));
        data.setServerId(rs.getInt("serverId"));
        String value = rs.getString("value");
        if(!StringUtils.isNullOrEmpty(value)) {
            data.setValue(new BigInteger(value));
        }
        return data;
    }

    public static GoodsInfo initGoodsInfo(ResultSet rs) throws SQLException {
        GoodsInfo data = new GoodsInfo();
        data.setGoodsId(rs.getInt("id"));
        data.setType(rs.getInt("type"));
        data.setQuality(rs.getInt("quality"));
        data.setCanUse(rs.getInt("canUse"));
        data.setTopLimit(rs.getInt("maxNum"));
        data.setSort(rs.getInt("sort"));
        data.setParamStr(rs.getString("params"));
        data.setParamList(StringUtils.stringToBigIntegerList(data.getParamStr(), "\\|"));
        data.setGoodsName(rs.getString("name"));
        data.setGoodsDesc(rs.getString("desc"));
        data.setIcon(rs.getString("icon"));
        data.setGetWay(rs.getString("getWay"));
        data.setExtendParam(rs.getString("extendParam"));
        data.setResourceParam(rs.getString("resourceParam"));
        data.setHidden(rs.getBoolean("isHidden"));
        data.setCanBagUse(rs.getBoolean("canBagUse"));
        data.setRecyclingType(rs.getInt("recyclingType"));
        return data;
    }

    public static TradeWarWanted initTradeWarWanted(ResultSet rs) throws SQLException {
        TradeWarWanted data = new TradeWarWanted();
        data.setWantedUid(rs.getString("wantedUid"));
        data.setUserId(rs.getLong("userId"));
        String baseUserInfoStr = rs.getString("userBaseInfo");
        if(null == baseUserInfoStr){
            baseUserInfoStr = "";
        }
        UserBaseInfo userBaseInfo = JSON.parseObject(baseUserInfoStr, new TypeReference<UserBaseInfo>() {});
        if(null != userBaseInfo) {
            data.setUserBaseInfo(userBaseInfo);
        }
        data.setWantedUserId(rs.getLong("wantedUserId"));
        baseUserInfoStr = rs.getString("wantedBaseInfo");
        if(null == baseUserInfoStr){
            baseUserInfoStr = "";
        }
        userBaseInfo = JSON.parseObject(baseUserInfoStr, new TypeReference<UserBaseInfo>() {});
        if(null != userBaseInfo) {
            data.setWantedBaseInfo(userBaseInfo);
        }
        data.setWantedScore(rs.getLong("wantedScore"));
        data.setWantedTime(rs.getLong("wantedTime"));
        data.setBattleTimes(rs.getInt("battleTimes"));
        data.setReward(rs.getString("reward"));
        data.setFinishTime(rs.getLong("finishTime"));
        data.setScoreReward(rs.getBoolean("scoreReward"));
        data.setUseItemId(rs.getInt("useItemId"));
        return data;
    }

    public static TradeWarTrackRecord initTradeWarTrackRecord(ResultSet rs) throws SQLException {
        TradeWarTrackRecord data = new TradeWarTrackRecord();
        data.setRecordUid(rs.getString("recordUid"));
        data.setWantedUid(rs.getString("wantedUid"));
        data.setUserId(rs.getLong("userId"));
        String baseUserInfoStr = rs.getString("userBaseInfo");
        if(null == baseUserInfoStr){
            baseUserInfoStr = "";
        }
        UserBaseInfo userBaseInfo = JSON.parseObject(baseUserInfoStr, new TypeReference<UserBaseInfo>() {});
        if(null != userBaseInfo) {
            data.setUserBaseInfo(userBaseInfo);
        }
        data.setKillPatronsCount(rs.getInt("killPatronsCount"));
        data.setLoseScore(rs.getInt("loseScore"));
        data.setRecordTime(rs.getLong("recordTime"));
        data.setMyAddScore(rs.getInt("myAddScore"));
        data.setTradeWarMoney(rs.getInt("tradeWarMoney"));
        return data;
    }
}
